﻿using LY.Manage.IBLL;
using LY.Manage.Model;
using LY.Manage.Model.EnumType;
using LY.Manage.Model.SearchParam;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LY.Manage.BLL
{
    public partial class UserInfoService:BaseService<UserInfo>,IUserInfoService
    {
        //public override void SetCurrentDal()
        //{
        //    CurrentDal = this.CurrentDbSession.UserInfoDal;
        //}
        public void SetUserInfo(UserInfo userInfo)
        {
            this.CurrentDbSession.UserInfoDal.AddEntity(userInfo);
            this.CurrentDbSession.UserInfoDal.DeleteEntity(userInfo);
            this.CurrentDbSession.UserInfoDal.UpdateEntity(userInfo);
            this.CurrentDbSession.SaveChanges();

        }

        /// <summary>
        /// 批量删除多条用户数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool DeleteEntityes(List<int> list)
        {
            var userInfoList = this.CurrentDbSession.UserInfoDal.LoadEntities(
                u => list.Contains(u.ID)
                    );
            foreach (var item in userInfoList)
            {
                this.CurrentDbSession.UserInfoDal.DeleteEntity(item);
            }
            return this.CurrentDbSession.SaveChanges();            
        }



        /// <summary>
        /// 多条件搜索用户信息
        /// </summary>
        /// <param name="userInfoParam"></param>
        /// <returns></returns>
        public   IQueryable<UserInfo> LoadSearchEntities(UserInfoParam userInfoParam)
        {
            short delFlag=(short)DeleteEnumType.Normarl;
            var temp = this.CurrentDbSession.UserInfoDal.LoadEntities(u => u.DelFlag == delFlag);
            if(!string.IsNullOrEmpty(userInfoParam.UserName     ))
            {
                temp = temp.Where<UserInfo>(u => u.UName.Contains( userInfoParam.UserName));
            }
            if (!string.IsNullOrEmpty(userInfoParam.Remark))
            {
                temp = temp.Where<UserInfo>(u => u.Remark.Contains(userInfoParam.Remark));
            }
            userInfoParam.TotalCount = temp.Count();

            return temp.OrderBy<UserInfo, int>(u => u.ID)
                .Skip<UserInfo>((userInfoParam.PageIndex - 1) * userInfoParam.PageSize)
                .Take<UserInfo>(userInfoParam.PageSize);

        }

        /// <summary>
        /// 为用户分配角色 利用导航属性
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        public bool SetUserRoleInfo(int userId, List<int> roleIdList)
        {
            var userInfo = this.CurrentDbSession.UserInfoDal.LoadEntities(
                u => u.ID == userId).FirstOrDefault();
            if (userInfo!=null)
            {
                //删除用户已有的角色 利用导航属性
                
                userInfo.RoleInfo.Clear();
                foreach (var roleId in roleIdList)
                {
                    var roleInfo=  this.CurrentDbSession.RoleInfoDal.LoadEntities(r => r.ID == roleId).FirstOrDefault();
                    userInfo.RoleInfo.Add(roleInfo);//给用户分配角色

                }
                return this.CurrentDbSession.SaveChanges();

            }
            else
            {
                return false;
            }

        }



        /// <summary>
        /// 完成用户权限的分配
        /// </summary>
        /// <param name="actionId"></param>
        /// <param name="userId"></param>
        /// <param name="isPass"></param>
        /// <returns></returns>
        public bool SetUserActionInfo(int actionId, int userId, bool isPass)
        {
            //判断userId以前是否有actionId的权限,如果有则更新isPass状态,否则插入
            var r_userInfo_actionInfo = this.CurrentDbSession.R_UserInfo_ActionInfoDal.LoadEntities(
                u => u.ActionInfoID == actionId && u.UserInfoID == userId).FirstOrDefault();
            if (r_userInfo_actionInfo==null)
            {
                //表示没有,插入
                R_UserInfo_ActionInfo userInfoActionInfo = new R_UserInfo_ActionInfo();
                userInfoActionInfo.ActionInfoID = actionId;
                userInfoActionInfo.UserInfoID = userId;
                userInfoActionInfo.IsPass = isPass;
                this.CurrentDbSession.R_UserInfo_ActionInfoDal.AddEntity(userInfoActionInfo);

            }
            else
            {
                //表示有,则更新
                r_userInfo_actionInfo.IsPass = isPass;
                this.CurrentDbSession.R_UserInfo_ActionInfoDal.UpdateEntity(r_userInfo_actionInfo);

            }
            return this.CurrentDbSession.SaveChanges();



        }
    }
}
